<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商品编码" prop="goodcode">
        <el-input
          v-model="queryParams.goodcode"
          placeholder="请输入商品编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodname">
        <el-input
          v-model="queryParams.goodname"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="款号" prop="gooditemnum">
        <el-input
          v-model="queryParams.gooditemnum"
          placeholder="请输入款号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="颜色" prop="goodcolor">
        <el-input
          v-model="queryParams.goodcolor"
          placeholder="请输入颜色"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="尺寸" prop="goodsize">
        <el-input
          v-model="queryParams.goodsize"
          placeholder="请输入尺寸"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类别" prop="goodclasstype">
        <el-select
          v-model="queryParams.goodclasstype"
          placeholder="请选择商品类别"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.sys_goods_goodClassType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="折扣" prop="discounttype">
        <el-input
          v-model="queryParams.discounttype"
          placeholder="请输入折扣"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="季节" prop="season">
        <el-select
          v-model="queryParams.season"
          placeholder="请选择商品季节"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.sys_goods_season"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:Goods:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:Goods:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:Goods:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:Goods:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="GoodsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />

      <el-table-column
        prop="goodattachmentaddress"
        label="图片"
        min-width="20%"
      >
        <!-- 图片的显示 -->
        <template slot-scope="scope">
          <img
            :src="scope.row.goodattachmentaddress"
            min-width="100"
            height="100"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品编码" align="center" prop="goodcode" />
      <el-table-column label="商品名称" align="center" prop="goodname" />
      <el-table-column label="款号" align="center" prop="gooditemnum" />
      <el-table-column label="颜色" align="center" prop="goodcolor" />
      <el-table-column label="尺寸" align="center" prop="goodsize" />
      <el-table-column label="商品类别" align="center" prop="goodclasstype">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_goods_goodClassType"
            :value="scope.row.goodclasstype"
          />
        </template>
      </el-table-column>
      <el-table-column label="季节" align="center" prop="season">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_goods_season"
            :value="scope.row.season"
          />
        </template>
      </el-table-column>
      <el-table-column label="铭牌价格" align="center" prop="nameplateprice" />
      <el-table-column label="销售价格" align="center" prop="saleprice" />
      <el-table-column label="成本" align="center" prop="costprice" />
      <el-table-column label="库存量" align="center" prop="stocknum" />
      <el-table-column label="折扣" align="center" prop="discounttype" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:Goods:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:Goods:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品录入对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片地址" prop="goodattachmentaddress">
          <el-input
            v-model="form.goodattachmentaddress"
            placeholder="请输入图片地址"
          />
        </el-form-item>
       <FileUpload>
         
       </FileUpload>

        <el-form-item label="商品编码" prop="goodcode">
          <el-input v-model="form.goodcode" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodname">
          <el-input v-model="form.goodname" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="款号" prop="gooditemnum">
          <el-input v-model="form.gooditemnum" placeholder="请输入款号" />
        </el-form-item>
        <el-form-item label="颜色" prop="goodcolor">
          <el-input v-model="form.goodcolor" placeholder="请输入颜色" />
        </el-form-item>
        <el-form-item label="尺寸" prop="goodsize">
          <el-input v-model="form.goodsize" placeholder="请输入尺寸" />
        </el-form-item>
        <el-form-item label="商品类别" prop="goodclasstype">
          <el-select v-model="form.goodclasstype" placeholder="请选择商品类别">
            <el-option
              v-for="dict in dict.type.sys_goods_goodClassType"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="季节" prop="season">
          <el-input v-model="form.season" placeholder="请输入季节" />
        </el-form-item>
        <el-form-item label="铭牌价格" prop="nameplateprice">
          <el-input
            v-model="form.nameplateprice"
            placeholder="请输入铭牌价格"
          />
        </el-form-item>
        <el-form-item label="销售价格" prop="saleprice">
          <el-input v-model="form.saleprice" placeholder="请输入销售价格" />
        </el-form-item>
        <el-form-item label="成本" prop="costprice">
          <el-input v-model="form.costprice" placeholder="请输入成本" />
        </el-form-item>
        <el-form-item label="库存量" prop="stocknum">
          <el-input v-model="form.stocknum" placeholder="请输入库存量" />
        </el-form-item>
        <el-form-item label="折扣" prop="discounttype">
          <el-select v-model="form.discounttype" placeholder="请选择折扣">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGoods,
  getGoods,
  delGoods,
  addGoods,
  updateGoods,
  exportGoods,
} from "@/api/system/Goods";
import { getToken } from "@/utils/auth";
export default {
  name: "Goods",
  dicts: ["sys_goods_goodClassType", "sys_goods_season"],
  data() {
    return {
      fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],
      headers: {
        Authorization: "Bearer " + getToken(),
      },
       //上传
      dialogImageUrl: "",
      headers: { Authorization: "Bearer " + getToken() },
      // 上传的地址
      file_upload_url: process.env.VUE_APP_BASE_API + "/common/upload",
      // 上传的文件列表
      fileList: [],
      dialogVisible: false,
      disabled: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品录入表格数据
      GoodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsattamentid: null,
        goodattachmentaddress: null,
        goodcode: null,
        goodname: null,
        gooditemnum: null,
        goodcolor: null,
        goodsize: null,
        goodclasstype: null,
        season: null,
        nameplateprice: null,
        saleprice: null,
        costprice: null,
        stocknum: null,
        discounttype: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
     
    };
  },
  created() {
    this.getList();
  },
  methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log("Bearer " + getToken());
        console.log(file);
      },

    /** 查询商品录入列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then((response) => {
        this.GoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsattamentid: null,
        goodattachmentaddress: null,
        goodcode: null,
        goodname: null,
        gooditemnum: null,
        goodcolor: null,
        goodsize: null,
        goodclasstype: null,
        season: null,
        nameplateprice: null,
        saleprice: null,
        costprice: null,
        stocknum: null,
        discounttype: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品录入";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGoods(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品录入";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.dialogImageUrl);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品录入编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有商品录入数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportGoods(queryParams);
        })
        .then((response) => {
          this.$download.name(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}
</style>